package com.fastwire.carriere.inventory.vo;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

import com.fastwire.carriere.common.constant.HuaweiInventoryConstants;
import com.fastwire.carriere.common.mapper.NENameMapper;
import com.fastwire.carriere.common.mapper.TPNameMapper;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.CrossConnectBandwidthDao;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.DAOFactory;
import com.fastwire.carriere.dao.NetworkInventoryDao;
import com.fastwire.carriere.inventory.entity.CrossConnect;
import com.fastwire.carriere.inventory.entity.CrossConnectBandwidth;
import com.fastwire.carriere.inventory.entity.FDFr;
import com.fastwire.carriere.inventory.entity.NetworkElement;
import com.fastwire.carriere.inventory.entity.NetworkEntityUserSpecificInfo;
import com.fastwire.carriere.inventory.entity.TransmissionParameter;

// TODO: Auto-generated Javadoc
/**
 * The Class CrossConnectVo.
 */
@Root(name = "CrossConnect")
public class CrossConnectVo {

	/** The oid. */
	@Attribute
	private String oid;

	/** The native ems name. */
	@Attribute
	private String nativeEmsName;

	/** The name. */
	@Attribute
	private String name;

	/** The aend tp. */
	@Attribute(required = false)
	private String aendTp;

	/** The zend tp. */
	@Attribute(required = false)
	private String zendTp;

	/** The is active. */
	@Attribute(required = false)
	private String isActive;

	/** The administrative state. */
	@Attribute(required = false)
	private String administrativeState;

	/** The direction. */
	@Attribute(required = false)
	private String direction;

	/** The is fixed. */
	@Attribute(required = false)
	private String isFixed;

	/** The ingress lsr id. */
	@Attribute(required = false)
	private String ingressLsrID;

	/** The egress lsr id. */
	@Attribute(required = false)
	private String egressLsrID;

	/** The bandwidth. */
	@Attribute(required = false)
	private String bandwidth;

	/** The pir. */
	@Attribute(required = false)
	private String pir;

	/** The next hop. */
	@Attribute(required = false)
	private String nextHop;

	@Attribute(required = false)
	private String remarks;

	@Attribute(required = true)
	private String type = "CrossConnect";

	/**
	 * Instantiates a new cross connect vo.
	 */
	public CrossConnectVo() {
		super();
	}

	/**
	 * Instantiates a new cross connect vo.
	 * 
	 * @param cc
	 *            the cc
	 */
	public CrossConnectVo(CrossConnect cc) {
		super();
		this.oid = String.valueOf(cc.getOid());
		this.nativeEmsName = cc.getEmsNativeName();
		this.name = cc.getName();
		NetworkElement ne;
		try {
			ne = NENameMapper.getNEbyU2kName(name);

			if (cc.getAendTp() != null) {
				aendTp = TPNameMapper.getReadableTpName(cc.getAendTp()
						.getName(), cc.getAendTp().getType(), ne);
			}

			if (cc.getZendTp() != null) {
				zendTp = TPNameMapper.getReadableTpName(cc.getZendTp()
						.getName(), cc.getZendTp().getType(), ne);
			}

			this.isActive = cc.getIsActive();
			this.administrativeState = cc.getAdministrativeState();
			this.direction = cc.getDirection();
			this.isFixed = cc.getIsFixed();

			TransmissionParameter transmissionParameter = cc
					.getTransmissionParameterbyLR(HuaweiInventoryConstants.LayerRate_MPLS_Path);
			this.egressLsrID = transmissionParameter
					.getParameter(HuaweiInventoryConstants.CC_EgressLsrID);
			this.ingressLsrID = transmissionParameter
					.getParameter(HuaweiInventoryConstants.CC_IngressLsrID);
			//This calls the CrossConnBW view. Take a long time
			setBandwidth();

			this.pir = transmissionParameter
					.getParameter(HuaweiInventoryConstants.CC_PIR);
			this.nextHop = transmissionParameter
					.getParameter(HuaweiInventoryConstants.CC_NextHop);
			
			//Commented as it queries for every Crossconnect and is not necessary
			/*
			if (!StringUtil.isEmptyString(nativeEmsName)) {

				NetworkInventoryDao inventoryDao = DAOFactory.getInstance()
						.getNetworkInventoryDao();
				NetworkEntityUserSpecificInfo info = inventoryDao
						.getNetworkEntityUserSpecificInfobyNameAndType(
								nativeEmsName, CrossConnect.class.getSimpleName());
				if (info != null) {
					remarks = info.getRemark();
				}
			}*/

		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Gets the oid.
	 * 
	 * @return the oid
	 */
	public String getOid() {
		return oid;
	}

	/**
	 * Sets the oid.
	 * 
	 * @param oid
	 *            the new oid
	 */
	public void setOid(String oid) {
		this.oid = oid;
	}

	/**
	 * Gets the native ems name.
	 * 
	 * @return the native ems name
	 */
	public String getNativeEmsName() {
		return nativeEmsName;
	}

	/**
	 * Sets the native ems name.
	 * 
	 * @param nativeEmsName
	 *            the new native ems name
	 */
	public void setNativeEmsName(String nativeEmsName) {
		this.nativeEmsName = nativeEmsName;
	}

	/**
	 * Gets the name.
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 * 
	 * @param name
	 *            the new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the aend tp.
	 * 
	 * @return the aend tp
	 */
	public String getAendTp() {
		return aendTp;
	}

	/**
	 * Sets the aend tp.
	 * 
	 * @param aendTp
	 *            the new aend tp
	 */
	public void setAendTp(String aendTp) {
		this.aendTp = aendTp;
	}

	/**
	 * Gets the zend tp.
	 * 
	 * @return the zend tp
	 */
	public String getZendTp() {
		return zendTp;
	}

	/**
	 * Sets the zend tp.
	 * 
	 * @param zendTp
	 *            the new zend tp
	 */
	public void setZendTp(String zendTp) {
		this.zendTp = zendTp;
	}

	/**
	 * Gets the checks if is active.
	 * 
	 * @return the checks if is active
	 */
	public String getIsActive() {
		return isActive;
	}

	/**
	 * Sets the checks if is active.
	 * 
	 * @param isActive
	 *            the new checks if is active
	 */
	public void setIsActive(String isActive) {
		this.isActive = isActive;
	}

	/**
	 * Gets the administrative state.
	 * 
	 * @return the administrative state
	 */
	public String getAdministrativeState() {
		return administrativeState;
	}

	/**
	 * Sets the administrative state.
	 * 
	 * @param administrativeState
	 *            the new administrative state
	 */
	public void setAdministrativeState(String administrativeState) {
		this.administrativeState = administrativeState;
	}

	/**
	 * Gets the direction.
	 * 
	 * @return the direction
	 */
	public String getDirection() {
		return direction;
	}

	/**
	 * Sets the direction.
	 * 
	 * @param direction
	 *            the new direction
	 */
	public void setDirection(String direction) {
		this.direction = direction;
	}

	/**
	 * Gets the checks if is fixed.
	 * 
	 * @return the checks if is fixed
	 */
	public String getIsFixed() {
		return isFixed;
	}

	/**
	 * Sets the checks if is fixed.
	 * 
	 * @param isFixed
	 *            the new checks if is fixed
	 */
	public void setIsFixed(String isFixed) {
		this.isFixed = isFixed;
	}

	/**
	 * Gets the ingress lsr id.
	 * 
	 * @return the ingress lsr id
	 */
	public String getIngressLsrID() {
		return ingressLsrID;
	}

	/**
	 * Sets the ingress lsr id.
	 * 
	 * @param ingressLsrID
	 *            the new ingress lsr id
	 */
	public void setIngressLsrID(String ingressLsrID) {
		this.ingressLsrID = ingressLsrID;
	}

	/**
	 * Gets the egress lsr id.
	 * 
	 * @return the egress lsr id
	 */
	public String getEgressLsrID() {
		return egressLsrID;
	}

	/**
	 * Sets the egress lsr id.
	 * 
	 * @param egressLsrID
	 *            the new egress lsr id
	 */
	public void setEgressLsrID(String egressLsrID) {
		this.egressLsrID = egressLsrID;
	}

	/**
	 * Gets the bandwidth.
	 * 
	 * @return the bandwidth
	 */
	public String getBandwidth() {
		return bandwidth;
	}

	/**
	 * Sets the bandwidth.
	 * 
	 * @param bandwidth
	 *            the new bandwidth
	 */
	public void setBandwidth(String bandwidth) {
		this.bandwidth = bandwidth;
	}

	public void setBandwidth() throws DAOException {
		long bw= 0L;
		try
		{
			bw= Long.parseLong(this.bandwidth);
			CommonLogger.info(this, "CC already has CC BW: " +bw);
		}
		catch(NumberFormatException exp)
		{
			CommonLogger.info(this, "CC don't have CC BW, So querying CCBW_VW" );
			CrossConnectBandwidthDao bandwidthDao = DAOFactory.getInstance()
			.getCrossConnectBandwidthDao();
			CrossConnectBandwidth ccBandwidth = bandwidthDao
			.getCrossConnectBandwidthbyName(nativeEmsName);

			if (ccBandwidth != null) {
				this.bandwidth = String.valueOf(ccBandwidth.getMaxbandwidth());
			}

		}		
	}

	/**
	 * Gets the pir.
	 * 
	 * @return the pir
	 */
	public String getPir() {
		return pir;
	}

	/**
	 * Sets the pir.
	 * 
	 * @param pir
	 *            the new pir
	 */
	public void setPir(String pir) {
		this.pir = pir;
	}

	/**
	 * Gets the next hop.
	 * 
	 * @return the next hop
	 */
	public String getNextHop() {
		return nextHop;
	}

	/**
	 * Sets the next hop.
	 * 
	 * @param nextHop
	 *            the new next hop
	 */
	public void setNextHop(String nextHop) {
		this.nextHop = nextHop;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
